package com.sms.graduate.controller;

import com.sms.graduate.entity.SmsClasscourse;
import com.sms.graduate.entity.SmsUser;
import com.sms.graduate.entity.dto.CourseDTO;
import com.sms.graduate.service.SmsClasscourseService;
import com.sms.graduate.service.SmsUserService;
import com.sms.graduate.utils.DataTools;
import com.sms.graduate.utils.R;
import com.sms.graduate.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/course")
@Api(tags = "课程模块")
@CrossOrigin(origins = "*")
public class CourseController {

    @Autowired
    SmsClasscourseService classcourseService;

    @Autowired
    SmsUserService smsUserService;

    @PostMapping("/selectCourse")
    @ApiOperation("查询课程")
    public R selectCourse(HttpServletRequest request){
        try {
            String userCode = TokenUtils.getUserCode(request);
            String s = DataTools.gradeByUserCode(userCode);
            String substring = s.substring(0, s.length() - 1);
            HashMap<Integer, List<SmsClasscourse>> map = DataTools.allCourse(substring);
            String[][] b=new String[8][6];
            map.forEach((key,value)->{
                key=key-1;
                if (value != null) {
                    for (SmsClasscourse smsClasscourse : value) {
                        int j =0;
                        b[key][j] = smsClasscourse.getCourseTime();
                        j++;
                        if (smsClasscourse.getDayWeek().equals("1")) {
                            b[key][j] = smsClasscourse.getCourse();
                        }
                        j++;
                        if (smsClasscourse.getDayWeek().equals("2")) {
                            b[key][j] = smsClasscourse.getCourse();
                        }
                        j++;
                        if (smsClasscourse.getDayWeek().equals("3")) {
                            b[key][j] = smsClasscourse.getCourse();
                        }
                        j++;
                        if (smsClasscourse.getDayWeek().equals("4")) {
                            b[key][j] = smsClasscourse.getCourse();
                        }
                        j++;
                        if (smsClasscourse.getDayWeek().equals("5")) {
                            b[key][j] = smsClasscourse.getCourse();
                        }
                    }
                }
            });
            List[] l = new List[8];
            int u= 0;
            for (String[] strings : b) {
                List<String> collect = Arrays.stream(strings).map(d -> {
                    if (d == null) {
                        return "";
                    }
                    return d;
                }).collect(Collectors.toList());
                l[u] = collect;
                u++;
            }
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("msg","查询成功");
            hashMap.put("data",l);
            return R.ok(hashMap);
        }catch (Exception e){
            e.getStackTrace();
            return R.error();
        }
    }

    @PostMapping("/updateAndAddCourse")
    @ApiOperation("修改课程表")
    public R updateAndAddCourse(@RequestBody List<CourseDTO> courses,@RequestParam("courseClass") String courseClass){
         try {
             for (int i = 1; i <=courses.size(); i++) {
                 CourseDTO course = courses.get(i-1);
                 classcourseService.updateAndAddCourse(course,courseClass,String.valueOf(i));
             }
             return R.ok("操作成功");
         }catch (Exception e){
             e.printStackTrace();
             return R.error();
         }
    }

    @PostMapping("/callTheRoll")
    @ApiOperation("随机点名")
    public R callTheRoll(@RequestParam(value = "num",required = false) Integer num,@RequestParam("userClass") String userClass){
        try {
            if (num == null){
                num = 5;
            }
            List<String> list = new ArrayList<>();
            HashSet<Integer> hashSet = new HashSet<>();
            List<SmsUser> smsUsers = smsUserService.selectStudentByClass(userClass);
            List<String> collect = smsUsers.stream().map(SmsUser::getUsername).collect(Collectors.toList());
            if (num<collect.size()){
                while (hashSet.size() < num){
                    int j = new Random().nextInt(collect.size());
                    hashSet.add(j);
                }
                for (Integer integer : hashSet) {
                    String s = collect.get(integer);
                    list.add(s);
                }
            }else {
                return R.ok("点名人数有误，请重新输入");
            }
            return R.ok().put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }





}
